﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Caching;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;

namespace YYS.Controllers
{
    public class HomeController : ControlerEx
    {
        
        #region 微信登录
        public static codeObj WxGetCode()//微信公众号获取code
        {
            string Appid = DefaultValue.AppID_WXCS;
            //string AppSecret = DefaultValue.AppSecret_WXCS;
            string wxUrl = "http://yangys.icu/yys/wx/go?state=userinfo";
            var url = $"https://open.weixin.qq.com/connect/oauth2/authorize?appid={Appid}&redirect_uri={wxUrl}&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect";
            var data = HttpGetByWebRequest(url, System.Text.Encoding.UTF8, 100);
            var json = data.xConvertJsonToObject<codeObj>();
            return json;
        }
        public static codeObj WxLogin(string code)//微信公众号登录
        {
            string Appid = DefaultValue.AppID_WXCS;
            string AppSecret = DefaultValue.AppSecret_WXCS;
            var url = $"https://api.weixin.qq.com/sns/oauth2/access_token?appid={Appid}&secret={AppSecret}&code={code}&grant_type=authorization_code";
            var data = HttpGetByWebRequest(url, System.Text.Encoding.UTF8, 100);
            var json = data.xConvertJsonToObject<codeObj>();
            return json;
        }
        public class codeObj
        {
            public string openid { get; set; }
            public string session_key { get; set; }
            public string unionid { get; set; }
            public int errcode { get; set; }
            public string errmsg { get; set; }
        }
        [HttpPost]
        public ActionResult WxMpLogin()
        {
            var code = Request.Form["code"].xTrim();
            string Appid = DefaultValue.AppID;
            string AppSecret = DefaultValue.AppSecret;
            var url = $"https://api.weixin.qq.com/sns/jscode2session?appid={Appid}&secret={AppSecret}&js_code={code}&grant_type=authorization_code";
            var data = HttpGetByWebRequest(url, System.Text.Encoding.UTF8, 100);
            var json = data.xConvertJsonToObject<codeObj>();
            //var a=getCurrentUser(json.openid);
            if (DefaultValue.IsAddUser)
            {
                //录入信息
                var optType = 0;
                var uiId = Request.Form["uiId"].xTo<int>();
                var uiName = Request.Form["uiName"].xTrim();
                var fId = Request.Form["fId"].xTrim();
                var account = Request.Form["account"].xTrim();
                var uId = Request.Form["uId"].xTrim();
                var cpu_id = Request.Form["cpu_id"].xTrim();
                var state = DefaultValue.state;
                var db = Helper.GETdbHelper(dbHelper.EnumServer.Local, dbHelper.ExecType.Exec, DefaultValue.DataBase);
                db.CmdText = "PR_PRC_userInfoSave";
                db.IsProc = true;
                db.AddParameter("@optType", SqlDbType.Int, optType);
                db.AddParameter("@uiId", SqlDbType.Int, uiId);
                db.AddParameter("@fId", SqlDbType.VarChar, fId, 50);
                db.AddParameter("@uId", SqlDbType.VarChar, uId, 50);
                db.AddParameter("@cpu_id", SqlDbType.NVarChar, cpu_id, 256);
                db.AddParameter("@state", SqlDbType.NVarChar, state, 50);
                db.AddParameter("@root", SqlDbType.Int, 0);
                db.AddParameter("@account", SqlDbType.NVarChar, account, 50);
                db.AddParameter("@uiName", SqlDbType.NVarChar, uiName, 50);
                db.AddParameter("@openId", SqlDbType.NVarChar, json.openid, 50);
                db.AddParameter("@session_key", SqlDbType.NVarChar, json.session_key, 50);
                db.AddParameter("@uiMark", SqlDbType.NVarChar, "", -1);
                db.AddParameter("@oiId", SqlDbType.NVarChar, "", 50);
                db.AddParameter("@xml", SqlDbType.NVarChar, "", -1);
                db.AddParameter("@imgUrl", SqlDbType.NVarChar, "", 128);
                db.AddParameter("@bucketName", SqlDbType.VarChar, "", 32);
                db.AddParameter("@email", SqlDbType.VarChar, "", 50);
                db.NeedOutError = true;
                db.Exec();
                if (db.HasError) return _DBERROR(db);
            }
            var optor = pcController.v3getCurrentUser(json.openid,1);
            if (optor.HasError) return backJson(1,optor.Error);
            return backJson(0, null, optor.Obj);
        }
        #endregion
        
    }
}
